//
//  LCScrollAttributeView.m
//  webSocket
//
//  Created by 奇艺果 on 2018/1/26.
//  Copyright © 2018年 奇艺果. All rights reserved.
//

#import "LCScrollAttributeView.h"
#import "LCAttributeCell.h"
#import "CMDollListModel.h"

static CGFloat AttributeViewMargin = 10.f;
static NSString *LCScrollAttributeCellID = @"LCScrollAttributeCellID";

@interface LCScrollAttributeView()<UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;

@property (nonatomic, weak) NSTimer *timer;
@property (nonatomic, assign) NSInteger totalCount;
@property (nonatomic, strong) UIImageView *tipImgView;
@property (nonatomic, strong) UIView *lineView;



@end


@implementation LCScrollAttributeView
#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        
        frame.size = CGSizeMake(SCREEN_WIDTH, ScaleW(40));
        [self viewWithcornerRadius:0 borderWidth:0 borderColor:nil backgroundColor:[UIColor whiteColor]];
        self.userInteractionEnabled = NO;
        [self addSubview:self.tipImgView];
        self.tipImgView.x = AttributeViewMargin;
        self.tipImgView.y = (frame.size.height - self.tipImgView.height - ScaleW(2)) * 0.5;
        self.totalCount = self.dataArray.count;
        CGFloat x = CGRectGetMaxX(self.tipImgView.frame) + AttributeViewMargin ;
        self.collectionView.frame = CGRectMake(x, 0, frame.size.width - x - AttributeViewMargin, ScaleW(38));
        [self addSubview:self.collectionView];
        
        [self addSubview:self.lineView];
        self.lineView.y = self.collectionView.height;
    }
    return self;
}

- (void)willMoveToSuperview:(UIView *)newSuperview
{
    if (!newSuperview) {
        [self invalidateTimer];
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.totalCount;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    long itemIndex = [self pageControlIndexWithCurrentCellIndex:indexPath.item];
    
    LCAttributeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:LCScrollAttributeCellID forIndexPath:indexPath];
    
    QYGNoticeModel *noticeModel  = self.dataArray[itemIndex];
    if(noticeModel){
        [cell attributeCellWithUName:noticeModel.nickname uIcon:noticeModel.avatar pName:noticeModel.ware_name pIcon:noticeModel.ware_image text:noticeModel.sentence];
    }
    return cell;
}

- (int)pageControlIndexWithCurrentCellIndex:(NSInteger)index
{
    return (int)index % self.dataArray.count;
}

#pragma mark - actions

- (void)setupTimer
{
    [self invalidateTimer]; // 创建定时器前先停止定时器，不然会出现僵尸定时器，导致轮播频率错误
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(automaticScroll) userInfo:nil repeats:YES];
    _timer = timer;
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)invalidateTimer
{
    [_timer invalidate];
    _timer = nil;
}

- (void)automaticScroll
{
    
    if (0 == self.totalCount) return;
    int currentIndex = [self currentIndex];
    int targetIndex = currentIndex + 1;
    if(targetIndex >= self.totalCount)
    {
        targetIndex = self.totalCount * 0.5;
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }else{
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:targetIndex inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    }
}

- (int)currentIndex
{
    if (self.width == 0 || self.height == 0) {
        return 0;
    }
    
    int index = 0;
    index = (self.collectionView.contentOffset.y + _flowLayout.itemSize.height * 0.5) / _flowLayout.itemSize.height;
    
    return MAX(0, index);
}
- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    
    [self.collectionView reloadData];
    if(dataArray.count > 1){
        self.totalCount = dataArray.count  * 100;
        self.collectionView.scrollEnabled = YES;
        [self setupTimer];
    }else{
        self.totalCount = dataArray.count;
        self.collectionView.scrollEnabled = NO;
        [self invalidateTimer];
    }
    if(dataArray.count){
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    }
}
#pragma mark - lazy
- (UICollectionViewFlowLayout *)flowLayout
{
    if(!_flowLayout)
    {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH - 2 * 12, ScaleW(38));
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        
    }
    return _flowLayout;
}

- (UICollectionView *)collectionView
{
    if(!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.flowLayout];
        _collectionView.dataSource = self;
        _collectionView.scrollsToTop = NO;
        _collectionView.backgroundColor = [UIColor clearColor];
        [_collectionView registerClass:[LCAttributeCell class] forCellWithReuseIdentifier:LCScrollAttributeCellID];
        _collectionView.pagingEnabled = YES;
    }
    return _collectionView;
}

- (UIImageView *)tipImgView{
    if(!_tipImgView){
        _tipImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"radio"]];
    }
    return _tipImgView;
}
- (UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = [UIColor colorWithHex:@"f0f0f0"];
        _lineView.size = CGSizeMake(SCREEN_WIDTH, 1);
//        _lineView.layer.shadowOffset = CGSizeMake(0, 5);
        _lineView.layer.shadowOpacity = 0.9;
        _lineView.layer.shadowColor = _lineView.backgroundColor.CGColor;
    }
    return _lineView;
}
@end
